package com.changpay.zywy.ui.activity.mine

import android.support.v4.app.FragmentTransaction
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.ProjectBean
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.dialog.SelectMonthDialog
import com.changpay.zywy.ui.dialog.SelectProjectDialog
import com.changpay.zywy.ui.fragment.mine.IncomeDetailFragment
import com.changpay.zywy.utils.DateUtil
import kotlinx.android.synthetic.main.activity_income_detail.*
import kotlinx.android.synthetic.main.title_layout.*
import org.jetbrains.anko.sdk27.coroutines.onClick

class IncomeDetailActivity : BaseActivity(), IView {


    private val TYPE_INDEX: String = "type_index"
    private val FRAGMENT_ONE = 0x01
    private val FRAGMENT_TWO = 0x02
    private val FRAGMENT_THREE = 0x03
    private val FRAGMENT_FOUR = 0x04
    private val FRAGMENT_FIVE = 0x05
    private val FRAGMENT_SIX = 0x06
    private val FRAGMENT_SEVENT = 0x07

    private var mIndex = FRAGMENT_ONE

    private var mOneFragment: IncomeDetailFragment? = null
    private var mTwoFragment: IncomeDetailFragment? = null
    private var mThreeFragment: IncomeDetailFragment? = null
    private var mFourFragment: IncomeDetailFragment? = null
    private var mFiveFragment: IncomeDetailFragment? = null
    private var mSixFragment: IncomeDetailFragment? = null
    private var mSevenFragment: IncomeDetailFragment? = null

    private var datas : List<ProjectBean> ?= null

    private var startTime = ""

    private var endTime = ""
    private var wallet_type = ""
    private var gc_id = "-1"  // 默认不窜

    private var dialogClickListener: SelectProjectDialog.OnDialogClickListener? = null

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {

    }

    override fun showMsg(msg: String) {

    }

    override fun showError(errorMsg: String) {

    }

    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
        }
    }

    override fun attachLayoutRes(): Int = R.layout.activity_income_detail

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "收益明细"
        wallet_type = intent.getStringExtra("wallet_type")
        iv_back.setOnClickListener(onClickListener)
        tv_check_time.setOnClickListener {
            val builder = SelectMonthDialog.Builder(this)
            builder.setOnDialogClickListener { v, month ->
                endTime = month.substring(0,4) + "-" +  month.substring(4,6)
                tv_select_time.text = month.substring(0,4) + "-" +  month.substring(4,6)
                tv_select_time.visibility = View.VISIBLE
                tv_back_month.text = "返回本月"
                loadData()
                reLoadData()

            }
            val build = builder.build<SelectMonthDialog>()
            build.isCancelable = true
            build.show(supportFragmentManager)
        }
        type_rg.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_one -> showFragment(FRAGMENT_ONE)
                R.id.rb_two -> showFragment(FRAGMENT_TWO)
                R.id.rb_three -> showFragment(FRAGMENT_THREE)
                R.id.rb_four -> showFragment(FRAGMENT_FOUR)
                R.id.rb_five -> showFragment(FRAGMENT_FIVE)
                R.id.rb_six -> showFragment(FRAGMENT_SIX)
                R.id.rb_seven -> showFragment(FRAGMENT_SEVENT)
            }
        }
        tv_back_month.onClick {
            if (tv_back_month.text != "本月")
            {
                tv_back_month.text = "本月"
                endTime = DateUtil.getYearMonth(System.currentTimeMillis())
                tv_select_time.visibility = View.INVISIBLE
                loadData()
                reLoadData()
            }
        }


        dialogClickListener = SelectProjectDialog.OnDialogClickListener { v, type ->
            gc_id = type.toString()
            loadData()
            reLoadGcidData()
        }


        tv_check_xiangmu.setOnClickListener {
            val builder = SelectProjectDialog.Builder(this)
            builder.setType(datas).setOnDialogClickListener(dialogClickListener)
            val build = builder.build<SelectProjectDialog>()
            build.show(supportFragmentManager)
        }

        if (wallet_type == "1")
        {
            showFragment(mIndex)
            rb_three.visibility = View.GONE
            rb_four.visibility = View.GONE
        }else
        {
            rb_one.visibility = View.GONE
            rb_two.visibility = View.GONE
            rb_six.visibility = View.GONE
            rb_seven.visibility = View.GONE
            mIndex = FRAGMENT_THREE
            type_rg.check(R.id.rb_three)
            showFragment(mIndex)
        }

    }

    private fun reLoadData() {
        mOneFragment?.setEndTime(endTime)
        mOneFragment?.reLoadData()
        mTwoFragment?.setEndTime(endTime)
        mTwoFragment?.reLoadData()
        mThreeFragment?.setEndTime(endTime)
        mThreeFragment?.reLoadData()
        mFourFragment?.setEndTime(endTime)
        mFourFragment?.reLoadData()
        mFiveFragment?.setEndTime(endTime)
        mFiveFragment?.reLoadData()
        mSixFragment?.setEndTime(endTime)
        mSixFragment?.reLoadData()
        mSevenFragment?.setEndTime(endTime)
        mSevenFragment?.reLoadData()

    }

    private fun reLoadGcidData() {
        mOneFragment?.setGcid(gc_id)
        mOneFragment?.reLoadData()
        mTwoFragment?.setGcid(gc_id)
        mTwoFragment?.reLoadData()
        mThreeFragment?.setGcid(gc_id)
        mThreeFragment?.reLoadData()
        mFourFragment?.setGcid(gc_id)
        mFourFragment?.reLoadData()
        mFiveFragment?.setGcid(gc_id)
        mFiveFragment?.reLoadData()
        mSixFragment?.setGcid(gc_id)
        mSixFragment?.reLoadData()
        mSevenFragment?.setGcid(gc_id)
        mSevenFragment?.reLoadData()

    }



    override fun start() {
        loadData()
    }

    private fun loadData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
//        param["search_date_star"] = startTime
        param["search_date_end"] = endTime
        param["wallet_type"] = wallet_type
        if (gc_id!="-1") param["gc_id"] = gc_id
        RetrofitHelper.service.earnings_detail(param).sss(this)
        {
            earn_detail_in.text = "￥${it.result.income}"
            earn_detail_out.text = "￥${it.result.spending}"
        }

        RetrofitHelper.service.getProject(param).sss(this)
        {
          datas = it.result
        }
    }


    /**
     * 隐藏所有的Fragment
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        mOneFragment?.let { transaction.hide(it) }
        mTwoFragment?.let { transaction.hide(it) }
        mThreeFragment?.let { transaction.hide(it) }
        mFourFragment?.let { transaction.hide(it) }
        mFiveFragment?.let { transaction.hide(it) }
        mSixFragment?.let { transaction.hide(it) }
        mSevenFragment?.let { transaction.hide(it) }
    }

    /**
     * 展示Fragment
     * @param index
     */
    private fun showFragment(index: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        mIndex = index
        when (index) {
            FRAGMENT_ONE //
            -> {
                if (mOneFragment == null) {
                    mOneFragment = IncomeDetailFragment.getInstance(0, startTime, endTime,wallet_type,gc_id)
                    transaction.add(R.id.container, mOneFragment!!, "one")
                } else {
                    transaction.show(mOneFragment!!)
                }
            }
            FRAGMENT_TWO //
            -> {
                if (mTwoFragment == null) {
                    mTwoFragment = IncomeDetailFragment.getInstance(1, startTime, endTime,wallet_type,gc_id)
                    transaction.add(R.id.container, mTwoFragment!!, "two")
                } else {
                    transaction.show(mTwoFragment!!)
                }
            }
            FRAGMENT_THREE //
            -> {
                if (mThreeFragment == null) {
                    mThreeFragment = IncomeDetailFragment.getInstance(2, startTime, endTime,wallet_type,gc_id)
                    transaction.add(R.id.container, mThreeFragment!!, "three")
                } else {
                    transaction.show(mThreeFragment!!)
                }
            }

            FRAGMENT_FOUR //
            -> {
                if (mFourFragment == null) {
                    mFourFragment = IncomeDetailFragment.getInstance(3, startTime, endTime,wallet_type,gc_id)
                    transaction.add(R.id.container, mFourFragment!!, "four")
                } else {
                    transaction.show(mFourFragment!!)
                }
            }

            FRAGMENT_FIVE //
            -> {
                if (mFiveFragment == null) {
                    mFiveFragment = IncomeDetailFragment.getInstance(4, startTime, endTime,wallet_type,gc_id)
                    transaction.add(R.id.container, mFiveFragment!!, "five")
                } else {
                    transaction.show(mFiveFragment!!)
                }
            }

            FRAGMENT_SIX //
            -> {
                if (mSixFragment == null) {
                    mSixFragment = IncomeDetailFragment.getInstance(5, startTime, endTime,wallet_type,gc_id)
                    transaction.add(R.id.container, mSixFragment!!, "six")
                } else {
                    transaction.show(mSixFragment!!)
                }
            }

            FRAGMENT_SEVENT //
            -> {
                if (mSevenFragment == null) {
                    mSevenFragment = IncomeDetailFragment.getInstance(6, startTime, endTime,wallet_type,gc_id)
                    transaction.add(R.id.container, mSevenFragment!!, "seven")
                } else {
                    transaction.show(mSevenFragment!!)
                }
            }
        }
        transaction.commit()
    }
}
